home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / builtin2.zoo / file.c < prev    next >
C/C++ Source or Header  |  1988-08-15  |  10KB  |  418 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25.  
  26. /* file.c */
  27.  
  28. #include "builtin.h"
  29. #ifndef AMIGA
  30. #include <netdb.h>
  31. #include <stdio.h>
  32. #endif
  33.  
  34. #define PMODE 0644
  35.  
  36. extern double floatval();
  37.  
  38. static int n, a, i;
  39. static int fileerrors = 0;    /* abort, or not on file errors */
  40. static struct psc_rec *user_psc, *stderr_psc, *ptr;
  41. static FILE *tempfile;
  42. static char s[256];
  43.  
  44. static word user_word, con_word, stderr_word;
  45.  
  46. struct ftab_ent {
  47.     int inout;        /* 1 if input, 0 if output */
  48.     word p_ptr;        /* tagged ptr to psc_ptr of constant */
  49.     FILE *fdes;        /* file descriptor for this constant */
  50.     };
  51.  
  52.     /* table of open files; 0 is always stdin,  1 is always stdout, 
  53.        2 is always stderr */
  54. static struct ftab_ent file_table[20];
  55. static int file_tab_end = 0; /* last used entry in file_table */
  56.  
  57.     /* index of current input (output) stream in file_table */
  58. static int in_file_i, out_file_i;
  59. extern FILE *curr_in, *curr_out;
  60.  
  61. static struct hostent *hp;
  62.  
  63.  
  64. int get_file_index(cword, io)
  65. word cword;
  66. int io;
  67. {
  68.     for (i=0; i<=file_tab_end; i++) {
  69.     if (file_table[i].p_ptr == cword)
  70.         if (io == file_table[i].inout || io >1) return(i);
  71.     }
  72.     return(-1);
  73. }
  74.  
  75. b_FILEERRORS()
  76. {
  77.         fileerrors = 1;
  78. }
  79.  
  80. b_NOFILEERRORS()
  81. {
  82.         fileerrors = 0;
  83. }
  84.  
  85. b_PUT() /* (N) */
  86.     register word op; 
  87.     register pw top;
  88.  
  89.     op = gregc(1); deref(op);
  90.     if (!isinteger(op)) {Fail0;}
  91.     else putc(intval(op), curr_out);
  92.     /* fflush(file_table[out_file_i].fdes); */
  93. }
  94.  
  95. b_GET0()  /* (N) */
  96. {
  97.     register word op;
  98.     register pw top;
  99.  
  100.     n = getc(curr_in);
  101.     if (n == EOF) clearerr(curr_in);
  102.     op = gregc(1); deref(op);
  103.     if (isnonvar(op)) {if (!unify(op, makeint(n))) {Fail0;}}
  104.     else {follow(op) = makeint(n); pushtrail(op);}
  105. }
  106.  
  107. b_GET()  /* (N) */
  108. {
  109.     register word op;
  110.     register pw top;
  111.  
  112.     do n = getc(curr_in);
  113.      while (n != EOF && n < 16 && n >= 112);
  114.     if (n == EOF) {
  115.         clearerr(curr_in);
  116.         Fail0; return;
  117.         }
  118.     op = gregc(1); deref(op);
  119.     if (isnonvar(op)) {if (!unify(op, makeint(n))) {Fail0;}}
  120.     else {follow(op) = makeint(n); pushtrail(op);}
  121. }
  122.  
  123. b_SKIP()
  124. {  /* ( N) */
  125.     register word op;
  126.     register pw top;
  127.  
  128.     op = gregc(1); deref(op);
  129.     if (!isinteger(op)) {Fail0; return;}
  130.     a = intval(op);
  131.     if (a < 16 || a >= 112) {Fail0;}
  132.     else {
  133.         do n = getc(curr_in);
  134.         while (n != EOF && n != a);
  135.         if (n = EOF) {
  136.         if (fileerrors) quit("end of file encountered.\n");
  137.         else {Fail0;}
  138.         }
  139.     }
  140. }
  141.  
  142. b_TAB()
  143. { /* (N) */
  144.     register word op;
  145.     register pw top;
  146.  
  147.     op = gregc(1); deref(op);
  148.     if (!isinteger(op)) {Fail0; return;}
  149.     a = intval(op);
  150.     if (a < 0) {Fail0; return;}
  151.     for ( ; a>0; a--) putc(' ', curr_out);
  152.     /* fflush(file_table[out_file_i].fdes); */
  153. }
  154.  
  155. b_NL()
  156. {    /* () */
  157.  
  158.         putc('\n', curr_out);
  159.         fflush(curr_out);
  160. }
  161.  
  162. b_WRITENAME()
  163. {     /* (X) */
  164.     register word op;
  165.     register pw top;
  166.  
  167.     op = gregc(1); 
  168.     wnd: switch ((int)(op&3)) {
  169.         case FREE:
  170.         nderef(op, wnd);
  171.         fprintf(curr_out, "_%d", untagged(op));
  172.         break;
  173.         case LIST:
  174.         fprintf(curr_out, ".");
  175.         break;
  176.         case CS:
  177.         ptr = get_str_psc(op);
  178.             if (get_etype(ptr) == T_BUFF) {
  179.            printf("Buffer_%x", get_name(ptr));
  180.             } 
  181.         else writepname(curr_out, get_name(ptr), get_length(ptr));
  182.         break;
  183.         case NUM:
  184.         if (isinteger(op))
  185.             fprintf(curr_out, "%d", intval(op));
  186.         else fprintf(curr_out, "%f", floatval(op));
  187.         break;
  188.     }
  189.     /* fflush(file_table[out_file_i].fdes); */
  190. }
  191.  
  192. b_WRITEQNAME()
  193. {     /* (X) */
  194.     register word op;
  195.     register pw top;
  196.  
  197.     op = gregc(1); 
  198.     wnd: switch ((int)(op&3)) {
  199.         case FREE:
  200.         nderef(op, wnd);
  201.         fprintf(curr_out, "_%d", untagged(op));
  202.         break;
  203.         case LIST:
  204.         fprintf(curr_out, ".");
  205.         break;
  206.         case CS:
  207.         ptr = get_str_psc(op);
  208.             if (get_etype(ptr) == T_BUFF) {
  209.            printf("Buffer_%x", get_name(ptr));
  210.             } 
  211.         else writeqname(curr_out, get_name(ptr), get_length(ptr));
  212.         break;
  213.         case NUM:
  214.         if (isinteger(op))
  215.             fprintf(curr_out, "%d", intval(op));
  216.         else fprintf(curr_out, "%f", floatval(op));
  217.         break;
  218.     }
  219.     /* fflush(file_table[out_file_i].fdes); */
  220. }
  221.  
  222. b_RESET()  /* () */
  223. {
  224.     quit("RESET not implemented\n");
  225.     
  226. /*    fop = gregc(1);
  227.     get_file_psc();
  228.     if (p == user_psc) set_file_ptr(p, stdin);
  229.     else {
  230.         namestring(p, s);
  231.         set_file_ptr(p, fopen(s, "r"));
  232.         if (get_file_ptr(p)==0) {Fail0;}
  233.     } */
  234. }
  235.  
  236. b_REWRITE()
  237. {
  238.     quit("REWRITE not implemented\n");
  239. /*
  240.     fop = gregc(1);
  241.     get_file_psc();
  242.     if (p == user_psc) set_file_ptr(p, stdout);
  243.     else {
  244.         namestring(p, s);
  245.         set_file_ptr(p, fopen(s, "w"));
  246.         if (get_file_ptr(p)==0) {Fail0;}
  247.     } */
  248. }
  249.  
  250. b_CLOSE()
  251. {
  252.     register word fop;
  253.     register pw top;
  254.  
  255.     fop = gregc(1); deref(fop);
  256.     i = get_file_index(fop, 2);
  257.     if (i>1) {  /* not user */
  258.         fclose(file_table[i].fdes);
  259.         for ( ; i<file_tab_end; i++) {
  260.         file_table[i] = file_table[i+1];
  261.         }
  262.         file_tab_end--;
  263.     }
  264. }
  265.  
  266. b_SEE()  /* r1: file name */
  267. {
  268.     register word fop;
  269.     register pw top;
  270.     int temp_in_file_i;
  271.  
  272.     fop = gregc(1); deref(fop);
  273.     temp_in_file_i = get_file_index(fop, 1);
  274.     if (temp_in_file_i<0) {  /* not in table */
  275.         namestring(get_str_psc(fop), s);
  276.         tempfile = fopen(s, "r");
  277.         if (!tempfile) {Fail0; return;} /* leaving in_file_i unchanged */
  278.         in_file_i = ++file_tab_end;
  279.         file_table[in_file_i].inout = 1;
  280.         file_table[in_file_i].p_ptr = fop;
  281.         file_table[in_file_i].fdes = tempfile;
  282.     }
  283.     else in_file_i = temp_in_file_i; /* take it from table */
  284.     curr_in = file_table[in_file_i].fdes;
  285. }
  286.  
  287. b_TELL()  /* r1: file name */
  288.       /* r2: 0 -> open `w'-write; 1 -> open `a'-append */
  289. {
  290.     register word sop, fop;
  291.     register pw top;
  292.  
  293.     fop = gregc(1); deref(fop);
  294.     sop = gregc(2); deref(sop);
  295.     out_file_i = get_file_index(fop, 0);
  296.     if (out_file_i<0) {  /* not in table */
  297.         namestring(get_str_psc(fop), s);
  298.         if(intval(sop)) tempfile = fopen(s, "a");
  299.         else tempfile = fopen(s, "w");
  300.         if (!tempfile) {Fail0; return;}
  301.         out_file_i = ++file_tab_end;
  302.         file_table[out_file_i].inout = 0;
  303.         file_table[out_file_i].p_ptr = fop;
  304.         file_table[out_file_i].fdes = tempfile;
  305.     };
  306.     curr_out = file_table[out_file_i].fdes;
  307. }
  308.  
  309. b_SEEING()  /* r1: unified with the current input file name */
  310. {
  311.     if (!unify(gregc(1), file_table[in_file_i].p_ptr)) {Fail0;}
  312. }
  313.  
  314. b_TELLING()  /* r1: unified with the current out put file name */
  315. {
  316.     if (!unify(gregc(1), file_table[out_file_i].p_ptr)) {Fail0;}
  317. }
  318.  
  319. b_SEEN()
  320. {
  321.     if (in_file_i > 2) {
  322.     fclose(curr_in);
  323.     for ( ; in_file_i<file_tab_end; in_file_i++) {
  324.         file_table[in_file_i] = file_table[in_file_i+1];
  325.     }
  326.     file_tab_end--;
  327.     }
  328.     in_file_i = 0; /* reset to user */
  329.     curr_in = file_table[in_file_i].fdes;
  330. }
  331.  
  332. b_TOLD()
  333. {
  334.     if (out_file_i > 2) {
  335.     fclose(file_table[out_file_i].fdes);
  336.     for ( ; out_file_i<file_tab_end; out_file_i++) {
  337.         file_table[out_file_i] = file_table[out_file_i+1];
  338.     }
  339.     file_tab_end--;
  340.     }
  341.     out_file_i = 1; /* reset to user */
  342.     curr_out = file_table[out_file_i].fdes;
  343. }
  344.  
  345.  
  346. b_GETHOSTBYNAME() /*     r1 is a constant indicating the host name, 
  347.             r2 is a namebuffer (of length 16) returned */
  348. {
  349.     register word op1, op2;
  350.     register pw top;
  351.  
  352. #ifdef AMIGA
  353.     printf("Error, gethostbyname has not been ported in this version\n");
  354.     printf("Change file builtin/file.c to add this function\n");
  355.     Fail0;
  356. #else    
  357.     op1 = gregc(1); deref(op1);
  358.     op2 = gregc(2); deref(op2);
  359.     namestring(get_str_psc(op1), s);
  360.     hp = gethostbyname(s);
  361.     bcopy(hp->h_addr, get_name(get_str_psc(op2))+4, hp->h_length);
  362. #endif
  363. }
  364.     
  365.  
  366. file_init()
  367. {
  368.     word temp;
  369.     char perm = 1;
  370.     char arity = 0;
  371.  
  372.     temp = insert("user", 4, arity, &perm);
  373.     user_psc = (struct psc_rec *)(follow(temp));
  374.     user_word = temp | CS_TAG;
  375.  
  376.     temp = insert("stderr", 6, arity, &perm);
  377.     stderr_psc = (struct psc_rec *)(follow(temp));
  378.     stderr_word = temp | CS_TAG;
  379.  
  380.     file_table[0].inout = 1;
  381.     file_table[0].p_ptr = user_word;
  382.     file_table[0].fdes = stdin;
  383.     in_file_i = 0;
  384.     curr_in = stdin;
  385.  
  386.     file_table[1].inout = 0;
  387.     file_table[1].p_ptr = user_word;
  388.     file_table[1].fdes = stdout;
  389.     out_file_i = 1;
  390.     curr_out = stdout;
  391.  
  392.     file_table[2].inout = 0;
  393.     file_table[2].p_ptr = stderr_word;
  394.     file_table[2].fdes = stderr;
  395.  
  396.     file_tab_end = 2;
  397. }
  398.  
  399. b_WRITE4()
  400. {    /* register 1 contains a bit string that is written out in 4 bytes */
  401.  
  402.     register word op, wbyte;
  403.     register pw top;
  404.     int i;
  405.  
  406.     op = gregc(1); deref(op);
  407.     for (i = 1; i <= 4; i++)
  408.     {    wbyte = ((op & 0xff000000)>>24);
  409.     op = op << 8;
  410.     putc(wbyte, curr_out);
  411.     }
  412.     return;
  413. }
  414.  
  415.  
  416.  
  417.